package servlet;

import dbOperation.DbInsert;
import library.Book;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by v on 17-5-20.
 */
@WebServlet(name = "insertServlet",value = "/insertServlet")
public class InsertServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            Book book=new Book();
            book.setName(req.getParameter("name"));
            book.setPrice(Double.valueOf(req.getParameter("price")));
            book.setAmount(Integer.valueOf(req.getParameter("amount")));
            book.setAuthor(req.getParameter("author"));
            DbInsert.insert(book);
        }catch (Exception ex){
            resp.sendRedirect("/exception/insertException.html");
        }
        resp.sendRedirect("/accept/insertAccept.html");
    }
}
